package com.biom3d.dashboard;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.commons.io.FilenameUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.biom3d.R;

import com.biom3d.filechooser.FileChooser;

public class ImportFileActivity extends Activity {

	Button buttonImport;
	EditText textName;
	EditText textCategory;
	EditText textDescription;
	final int FILE_CHOOSER = 1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dashboard_import_layout);

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		if (dm.widthPixels < 600) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}

		buttonImport = (Button) findViewById(R.id.btn_import);
		textName = (EditText) findViewById(R.id.EditTextName);
		textCategory = (EditText) findViewById(R.id.EditTextCategory);
		textDescription = (EditText) findViewById(R.id.EditTextDescription);

		buttonImport.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				//String name = textName.getText().toString();
				//String category = textCategory.getText().toString();
				//String description = textDescription.getText().toString();

				if (textName.getText().toString().length() == 0) {
					textName.setError("Enter Name");
				} else if (textCategory.getText().length() == 0) {
					textCategory.setError("Enter Category");
				} else {
					// Launching File Chooser
					Intent intent = new Intent(getApplicationContext(),
							FileChooser.class);
					ArrayList<String> extensions = new ArrayList<String>();
					extensions.add(".obj");
					// extensions.add(".mtl");
					intent.putStringArrayListExtra("filterFileExtension",
							extensions);
					startActivityForResult(intent, FILE_CHOOSER);
				}

			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		if ((requestCode == FILE_CHOOSER) && (resultCode == -1)) {
			String fileSelected = data.getStringExtra("fileSelected");
			String fileName = FilenameUtils.getName(fileSelected);
			File f = new File(fileSelected);

			// Copiando OBJ
			System.out.println("OBJ Filepath:" + fileSelected);
			System.out.println("OBJ Filename:" + fileName);
			if (f.isFile())
				copyfile(fileSelected, fileName);
			else
				return;

			// Copiando MTL
			fileSelected = FilenameUtils.getPath(fileSelected)
					+ FilenameUtils.getBaseName(fileSelected) + ".mtl";
			String preFileName = FilenameUtils.getPath(fileSelected)
					+ FilenameUtils.getBaseName(fileSelected) + "_mtl";
			// fileName = FilenameUtils.getName(fileSelected);
			fileName = FilenameUtils.getName(preFileName);

			System.out.println("MTL Filepath:" + fileSelected);
			System.out.println("MTL Filename:" + fileName);
			f = new File(fileSelected);
			if (f.isFile())
				copyfile(fileSelected, fileName);

			// Copiando texturas (JPG)
			fileSelected = FilenameUtils.getPath(fileSelected)
					+ FilenameUtils.getBaseName(fileSelected) + ".jpg";
			fileName = FilenameUtils.getName(fileSelected);
			System.out.println("JPG Filepath:" + fileSelected);
			System.out.println("JPG Filename:" + fileName);
			f = new File(fileSelected);
			if (f.isFile())
				copyfile(fileSelected, fileName);
			
			
			// Copiando texturas (PNG)
			fileSelected = FilenameUtils.getPath(fileSelected)
					+ FilenameUtils.getBaseName(fileSelected) + ".png";
			fileName = FilenameUtils.getName(fileSelected);
			System.out.println("PNG Filepath:" + fileSelected);
			System.out.println("PNG Filename:" + fileName);
			f = new File(fileSelected);
			if (f.isFile())
				copyfile(fileSelected, fileName); 

			Toast.makeText(this,
					"File '" + FilenameUtils.getBaseName(fileSelected) + ".obj' loaded successfully!",
					Toast.LENGTH_LONG).show();

			Intent i = new Intent(getApplicationContext(),
					OpenImageActivity.class);
			startActivity(i);
		}
	}

	private void copyfile(String srFile, String dtFile) {

		try {

			File f1 = new File(srFile);
			// File f2 = new File(dtFile);

			InputStream in = new FileInputStream(f1);

			// For Append the file.
			// OutputStream out = new FileOutputStream(f2,true);
			// For Overwrite the file.
			// OutputStream out = new FileOutputStream(f2);

			FileOutputStream out = openFileOutput(dtFile, Context.MODE_PRIVATE);

			byte[] buf = new byte[1024];

			int len;

			while ((len = in.read(buf)) > 0) {

				out.write(buf, 0, len);

			}

			in.close();

			out.close();

			System.out.println("File copied");

		}

		catch (FileNotFoundException ex) {

			System.out.println(ex.getMessage()
					+ " in  the specified directory.");

			System.exit(0);

		}

		catch (IOException e) {

			System.out.println(e.getMessage());

		}

	}
}